package cn.hg.jfsa.manage.directive;

import cn.hg.base.jfinal.directive.BaseDirective;
import cn.hg.base.jfinal.directive.annotation.JfsaDirective;
import cn.hg.jfsa.model.JfsaAdmin;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

@JfsaDirective("admin")
public class AdminDirective extends BaseDirective {

	/* (non-Javadoc)
	 * @see cn.hg.base.jfinal.directive.BaseDirective#onRender(com.jfinal.template.Env, com.jfinal.template.stat.Scope, com.jfinal.template.io.Writer)
	 */
	@Override
	public void onRender(Env env, Scope scope, Writer writer) {
		Integer id = this.getPara("id", scope);
		if (id == null) {
			throw new IllegalArgumentException("#admin(id=...) argument must not be empty");
		}

		// 从缓存中取得Bean
		JfsaAdmin bean = CacheKit.get("admin", "admin_" + id, new IDataLoader() {
			@Override
			public Object load() {
				return JfsaAdmin.DAO.findById(id);
			}
		});

		scope.setLocal("admin", bean);
		renderBody(env, scope, writer);
	}

	@Override
	public boolean hasEnd() {
		return true;
	}

}
